<?php

namespace AuctionCore\OilPayOrder;

class OilPayOrder
{

    public static function getOrderList($page, $pageSize, $params = [])
    {
        $condition = [];
        if ($params['status']) {
            if ($params['status'] == 2) {
                $condition['o.status'] = ['between', [2, 3]];
            } else {
                $condition['o.status'] = $params['status'];
            }
        }

        if ($params['order_sn']) {
            $condition['o.order_sn'] = $params['order_sn'];
        }

        if ($params['juhe_order_sn']) {
            $condition['o.juhe_order_sn'] = $params['juhe_order_sn'];
        }

        if ($params['pay_code']) {
            $condition['o.pay_code'] = $params['pay_code'];
        }

        if ($params['card_type']) {
            $condition['o.card_type'] = $params['card_type'];
        }

        if ($params['card_num']) {
            $condition['o.card_num'] = $params['card_num'];
        }

        if ($params['nick_name']) {
            $condition['u.nick_name'] = $params['nick_name'];
        }


        if ($params['start_time'] && $params['end_time']) {
            $condition['o.create_time'] = ['between', [strtotime($params['start_time']), strtotime($params['end_time'])]];
        } elseif ($params['start_time']) {
            $condition['o.create_time'] = ['gt', strtotime($params['start_time'])];
        } elseif ($params['end_time']) {
            $condition['o.create_time'] = ['lt', strtotime($params['end_time'])];
        }

        $total = M('oil_pay_order o', 'clcw_', 'DB_CLCW')
            ->join('left join clcw_user u on o.user_id=u.user_id')
            ->where($condition)->count();

        $list = M('oil_pay_order o', 'clcw_', 'DB_CLCW')
            ->join('left join clcw_user u on o.user_id=u.user_id')
            ->field('o.id,o.order_sn,o.juhe_order_sn,o.pay_sn,o.pay_code,o.money,o.status,o.card_type,o.card_num,o.user_id,u.nick_name,o.create_time,o.return_time')
            ->where($condition)->page($page, $pageSize)->select();

        return ['total' => $total, 'list' => $list];

    }


    public static function getOrderById($id)
    {
        return M('oil_pay_order', 'clcw_', 'DB_CLCW')->where(['id' => $id])->find();
    }

    public static function getOrderBySn($sn)
    {
        return M('oil_pay_order', 'clcw_', 'DB_CLCW')->where(['order_sn' => $sn])->find();
    }

    public static function saveOrderBySn($sn, $data)
    {
        return M('oil_pay_order', 'clcw_', 'DB_CLCW')->where(['order_sn' => $sn])->save($data);
    }

    public static function saveOrderById($id, $data)
    {
        return M('oil_pay_order', 'clcw_', 'DB_CLCW')->where(['id' => $id])->save($data);
    }


}